package com.hhf.rpa.job.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hhf.rpa.job.entity.BaseJob;
import com.hhf.rpa.job.mapper.BaseJobMapper;
import com.hhf.rpa.job.pojo.form.BaseJobForm;
import com.hhf.rpa.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
public class BaseJobServiceImpl extends ServiceImpl<BaseJobMapper, BaseJob> implements BaseJobService {

    @Override
    public List<BaseJob> getJob() {
        QueryWrapper<BaseJob> qw = new QueryWrapper();
        qw.eq("state", 1);
        return list(qw);
    }

    @Override
    public BaseJob getJobById(Integer jobId) {
        return getById(jobId);
    }

    @Override
    public IPage<BaseJob> getJobPage(BaseJobForm form) {
        IPage page = new Page(form.getCurrent(), form.getSize());
        QueryWrapper qw = new QueryWrapper();
        Integer jobId = form.getJobId();
        String jobName = form.getJobName();
        Integer state = form.getState();
        qw.eq(jobId != null, "JOB_ID", jobId);
        qw.like(StringUtils.isNotBlank(jobName), "JOB_NAME", jobName);
        qw.eq(state != null, "STATE", state);
        qw.orderByDesc(new Object[]{"STATE", "CREATED"});
        return baseMapper.selectPage(page,qw);
    }

    @Override
    public boolean updateJobExecStatus(Integer jobId, Integer from, Integer to) {
        UpdateWrapper<BaseJob> uw = new UpdateWrapper<>();
        uw.eq(jobId != null, "job_id", jobId);
        uw.eq(from != null, "exec_status", from);
        uw.set("exec_status", to);
        return update(uw);
    }

    @Override
    public boolean updateNextExecTime(Integer jobId, Date date) {
        UpdateWrapper<BaseJob> uw = new UpdateWrapper<>();
        uw.eq("job_id", jobId);
        uw.set("next_exec_time", date);
        return update(uw);
    }

    @Override
    public void saveJob(BaseJob baseJob) {
        if (baseJob.getJobId() == null) {
            baseJob.setCreated(LocalDateTime.now());
            baseJob.setExecStatus(0);
        }
        saveOrUpdate(baseJob);
    }

    @Override
    public void copyJob(Integer jobId) {
        BaseJob baseJob = getJobById(jobId);
        BaseJob newJob = new BaseJob();
        BeanUtils.copyProperties(baseJob, newJob);
        newJob.setJobName(baseJob.getJobName()+"_copy");
        newJob.setJobId(null);
        newJob.setState(0);
        this.saveJob(newJob);
    }

    @Override
    public List<BaseJob> getByParams(String param) {
        QueryWrapper<BaseJob> qw = new QueryWrapper();
        qw.like("params", param);
        return list(qw);
    }

    @Override
    public void deleteByParams(String param) {
        QueryWrapper<BaseJob> qw = new QueryWrapper();
        qw.like("params", param);
        remove(qw);
    }
}
